//
//  BIDEmailReceiveConditionViewController.m
//  shangwuting
//
//  Created by mal on 13-12-27.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDEmailReceiveConditionViewController.h"
#import "BIDGlobalMethods.h"
//接收情况
static NSString *strEmailReceiveConditionURL = @"emailAndroid!gainEmailRead.action";

@interface BIDEmailReceiveConditionViewController ()<BIDBaseListViewDelegate>
{
    NSArray *_readedArr;
    NSArray *_unReadArr;
}
@end

@implementation BIDEmailReceiveConditionViewController
@synthesize emailId;
@synthesize itemsDictionary;
@synthesize statusArr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"接收情况";
	// Do any additional setup after loading the view.
    self.myTableView.allowsSelection = NO;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], strEmailReceiveConditionURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", emailId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            _readedArr = [[NSArray alloc] initWithArray:responseDic[@"isRead"]];
            _unReadArr = [[NSArray alloc] initWithArray:responseDic[@"notsRead"]];
            [self.dataSourceArr addObject:@[]];
            [self.dataSourceArr addObject:@[]];
            [self.myTableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    itemsDictionary = nil;
    statusArr = nil;
}

- (void)tapGestureHandler:(UITapGestureRecognizer*)tapGR
{
    UIView *tapView = tapGR.view;
    NSInteger index = tapView.tag;
    NSArray *arr = self.dataSourceArr[index];
    if(arr.count==0)
    {
        if(index==0)
        {
            [self.dataSourceArr replaceObjectAtIndex:index withObject:_unReadArr];
        }
        else
        {
            [self.dataSourceArr replaceObjectAtIndex:index withObject:_readedArr];
        }
    }
    else
    {
        [self.dataSourceArr replaceObjectAtIndex:index withObject:@[]];
    }
    [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)btnDownHandler:(UIButton *)btn
{
    NSNumber *value = [statusArr objectAtIndex:btn.tag];
    if([value intValue]==1)
    {
        value = [NSNumber numberWithInt:0];
    }
    else
    {
        value = [NSNumber numberWithInt:1];
    }
    [statusArr replaceObjectAtIndex:btn.tag withObject:value];
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:btn.tag];
    [self.myTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSourceArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.dataSourceArr[section];
    return arr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    //已读
    static NSString *identifier1 = @"identifier1";
    //未读
    static NSString *identifier2 = @"identifier2";
    NSInteger section = [indexPath section];

    UITableViewCell *cell;
    NSArray *arr = self.dataSourceArr[section];
    NSDictionary *dic = arr[row];
    switch(section)
    {
        case 1:
        {
            //已读
            cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
            if(!cell)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier1];
            }
            cell.textLabel.text = [dic objectForKey:@"trueName"];
            cell.detailTextLabel.text = [dic objectForKey:@"readTime"];
        }
            break;
        case 0:
        {
            //未读
            cell = [tableView dequeueReusableCellWithIdentifier:identifier2];
            if(!cell)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier2];
            }
            cell.textLabel.text = [dic objectForKey:@"trueName"];
        }
            break;
    }
    [cell setBackgroundColor:[UIColor clearColor]];
    return cell;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = nil;
    headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 60.f)];
    headerView.userInteractionEnabled = YES;
    [headerView setBackgroundColor:[UIColor clearColor]];
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(headerView.frame), 50.f)];
    [subView setBackgroundColor:[UIColor whiteColor]];
    [headerView addSubview:subView];
    //
    UILabel *flagLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 7, 36, 36)];
    [flagLabel setTextAlignment:NSTextAlignmentCenter];
    NSString *strTitle = @"";
    if(section==0)
    {
        strTitle = [[NSString alloc] initWithFormat:@"未读人员(%ld)", _unReadArr.count];
    }
    else
    {
        strTitle = [[NSString alloc] initWithFormat:@"已读人员(%ld)", _readedArr.count];
    }
    NSString *strFlag = section==0?@"未":@"已";
    UIColor *color = section==0?RGBACOLOR(222.f, 122.f, 61.f, 1.f):RGBACOLOR(79.f, 158.f, 242.f, 1.f);
    [flagLabel setText:strFlag];
    [flagLabel setBackgroundColor:color];
    [flagLabel setFont:[UIFont systemFontOfSize:20.f]];
    [flagLabel setTextColor:[UIColor whiteColor]];
    flagLabel.layer.cornerRadius = CGRectGetHeight(flagLabel.frame)/2;
    flagLabel.clipsToBounds = YES;
    [subView addSubview:flagLabel];
    //
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(52, 0, 150, CGRectGetHeight(subView.frame))];
    [titleLabel setTextColor:[UIColor blackColor]];
    [titleLabel setFont:[UIFont systemFontOfSize:17.f]];
    [titleLabel setText:strTitle];
    //
    [subView addSubview:titleLabel];
    //
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(subView.frame)-0.5f, CGRectGetWidth(subView.frame), 0.5f)];
    [lineLabel setBackgroundColor:RGBACOLOR(179.f, 179.f, 179.f, 1.f)];
    [subView addSubview:lineLabel];
    //
    headerView.tag = section;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    [headerView addGestureRecognizer:tapGR];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60.0f;
}

@end
